package com.ltmonitor.web.controller.mobile;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.AlarmConfig;
import com.ltmonitor.entity.BasicData;
import com.ltmonitor.service.IAlarmConfigService;
import com.ltmonitor.web.controller.GenericAction;
import com.ltmonitor.web.util.JsonMessage;
/**
 * 配置报警类型是否启用，报警声音是否启用。
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/mobile")
public class MobileAlarmConfigAction extends GenericAction {

	@Autowired
	private IAlarmConfigService alarmConfigService;


	@ResponseBody
	@RequestMapping("/viewAlarmConfig.action")
	public JsonMessage view() {
		try {
			Collection<AlarmConfig> alarmTypes = alarmConfigService.getAlarmConfigByDepId(0);
			for(Object obj:alarmTypes)
			{
				AlarmConfig a = (AlarmConfig)obj;
				String sourceDescr = super.convert(a.getAlarmSource(), "AlarmSource");
				a.setAlarmSource(sourceDescr);
			}
			

			return json(true, alarmTypes);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return json(false,e.getMessage());
		}
	}
	

	@ResponseBody
	@RequestMapping("/alarmConfig.action")
	public JsonMessage config(long[] alarmEnable, long[] alarmSoundEnable,
			long[] alarmPopupEnable, long[] alarmStatisticEnable, Model m) {
		try {
			Collection<AlarmConfig> alarmConfigs = alarmConfigService.loadAll();
			// alarmTypes =
			// AlarmConfigService.getAlarmConfigByParentCode("AlarmType");
			HashMap<Long, Long> alarmEnableMap = new HashMap<Long, Long>();
			HashMap<Long, Long> soundEnableMap = new HashMap<Long, Long>();
			HashMap<Long, Long> popupEnableMap = new HashMap<Long, Long>();
			HashMap<Long, Long> statisticEnableMap = new HashMap<Long, Long>();
			if (alarmEnable != null) {
				for (Long typeId : alarmEnable) {
					alarmEnableMap.put(typeId, typeId);
				}
			}
			if (alarmSoundEnable != null) {
				for (Long typeId : alarmSoundEnable) {
					soundEnableMap.put(typeId, typeId);
				}
			}
			if (alarmPopupEnable != null) {
				for (Long typeId : alarmPopupEnable) {
					popupEnableMap.put(typeId, typeId);
				}
			}

			if (alarmStatisticEnable != null) {
				for (Long typeId : alarmStatisticEnable) {
					statisticEnableMap.put(typeId, typeId);
				}
			}

			for (AlarmConfig bd : alarmConfigs) {
				long typeId = bd.getEntityId();
				bd.setEnabled(alarmEnableMap.containsKey(typeId));
				bd.setSoundEnabled(soundEnableMap.containsKey(typeId));
				bd.setPopupEnabled(popupEnableMap.containsKey(typeId));
				bd.setStatisticEnabled(statisticEnableMap.containsKey(typeId));
			}
			this.alarmConfigService.saveOrUpdateAll(alarmConfigs);
			for (Object obj : alarmConfigs) {
				AlarmConfig a = (AlarmConfig) obj;
				String sourceDescr = super.convert(a.getAlarmSource(),
						"AlarmSource");
				a.setAlarmSource(sourceDescr);
			}
			return json(true, "设置成功");
			
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}



}
